package models

import (
	"strings"
	"time"
	"xorm.io/builder"
)

type Examiner struct {
	Id               int       `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	UserId           int       `xorm:"not null comment('用户id外键关联dts_user中的id') index INT(11)"`
	SchoolId         int       `xorm:"not null comment('驾校id外键关联dts_school中的自增id') index INT(11)"`
	IcId             int       `xorm:"default 0 comment('IC卡id,考核员可以没有IC卡也可以拥有多张IC卡，当前为生效IC卡id') INT(11)"`
	FingerId         int       `xorm:"default 0 comment('指纹id，考核员可以拥有多个指纹记录，当前为生效指纹id') INT(11)"`
	PortraitId       int       `xorm:"default 0 comment('头像id，考核员可以拥有多个头像记录，当前为生效头像id') INT(11)"`
	LicenceNumber    string    `form:"licence_number" xorm:"default '' comment('驾驶证号') VARCHAR(128)"`
	LicenceGrantDate time.Time `xorm:"comment('驾驶证初领日期 YYYYMMDD') DATE"`
	OccupationNumber string    `form:"occupation_number" xorm:"not null default '' comment('教练员职业资格证件号') VARCHAR(128)"`
	OccupationRank   string    `form:"occupation_rank" xorm:"default '1' comment('教练员职业资格等级，1：一级，2：二级，3：三级，4：四级') ENUM('1','2','3','4')"`
	DrivingCarType   string    `form:"driving_car_type" xorm:"default 'C1' comment('准驾车型') ENUM('A1','A2','A3','B1','B2','C1','C2','C3','C4','C5','D','E','F','M','N','P')"`
	TeachingCarType  string    `form:"teaching_car_type" xorm:"default 'C1' comment('准教车型') ENUM('A1','A2','A3','B1','B2','C1','C2','C3','C4','C5','D','E','F','M','N','P')"`
	EntryDate        time.Time `xorm:"comment('入职日期 YYYYMMDD') DATE"`
	DepartureDate    time.Time `xorm:"comment('离职日期 YYYYMMDD') DATE"`
	Status           string    `form:"status" xorm:"default '1' comment('0：在职, 1：离职') ENUM('0','1')"`
	ApprovalStatus   string    `xorm:"default '0' comment('考核员提交运管审核状态，0:待审核，1:通过审核，2:未通过审核') ENUM('0','1','2')"`
	RecordStatus     string    `xorm:"not null default '0' comment('备案状态 0:未备案 1:已备案') ENUM('0','1')"`
	InstitutionCode  string    `xorm:"comment('运管统一编号') VARCHAR(16)"`
	Remark           string    `xorm:"VARCHAR(255)"`
	Isused           string    `xorm:"default '1' comment('1在使用 0已删除') ENUM('0','1')"`
}

type ExaminerInfo struct {
	Id               int    `json:"id"`
	SchoolId         int    `json:"school_id"`
	Name             string `json:"name"`
	EntryDate        string `json:"entry_date"`
	DepartureDate    string `json:"departure_date"`
	Phone            string `json:"phone"`
	TeachingCarType  string `json:"teaching_car_type"`
	Status           string `json:"status"`
	ApprovalStatus   string `json:"approval_status"`
	RecordStatus     string `json:"record_status"`
	LicenceNumber    string `json:"licence_number"`
	LicenceGrantDate string `json:"licence_grant_date"`
	OccupationNumber string `json:"occupation_number"`
	OccupationRank   string `json:"occupation_rank"`
	DrivingCarType   string `json:"driving_car_type"`
	ImageUrl         string `json:"image_url"`
	PortraitCode     string `json:"portrait_code"`
	Nation           string `json:"nation"`
	Sex              string `json:"sex"`
	Nationality      string `json:"nationality"`
	Native           string `json:"native"`
	District         string `json:"district"`
	IdNumber         string `json:"id_number"`
	IdType           string `json:"id_type"`
	Address          string `json:"address"`
}

func (c *Examiner) GetExaminerList(school_id string, filter builder.Cond, pagesize int, offset int) (list []*ExaminerInfo, count int64, err error) {
	if err = engine.Table("dts_examiner").Alias("c").Select("c.id,c.school_id,c.status,c.approval_status,c.record_status,c.teaching_car_type,c.driving_car_type,c.entry_date,c.departure_date,c.licence_number,c.licence_grant_date,c.occupation_number,c.occupation_rank,u.name as Name,u.phone,u.nation,u.address,u.sex,u.nationality,u.district,u.id_number,u.id_type,u.native,dp.image_url,dp.institution_code as portrait_code").Join("LEFT", []string{"dts_user", "u"}, "c.user_id=u.id").Join("RIGHT", []string{"dts_portrait", "dp"}, "dp.id=c.portrait_id").Where("c.school_id=? and c.isused=?", school_id, "1").And(filter).Desc("c.id").Limit(pagesize, offset).Find(&list); err != nil {
		return list, 0, err
	}

	_, err = engine.Table("dts_examiner").Alias("c").Select("count(c.id) as count").Join("LEFT", []string{"dts_user", "u"}, "c.user_id=u.id").Where("c.school_id=? and c.isused=?", school_id, "1").And(filter).Get(&count)
	if err != nil {
		return list, count, err
	}
	for k, _ := range list {
		list[k].Phone = strings.Replace(list[k].Phone, "e", "", -1)
		list[k].IdNumber = strings.Replace(list[k].IdNumber, "e", "", -1)
	}
	return list, count, nil
}

type StageExaminer struct {
	Id              int
	InstitutionCode string
	Name            string
	ApprovalStatus  string
}

func (m *Examiner) GetExaminerBySchoolId(school_id int, province string) (stageex StageExaminer, err error) {
	session := engine.Table("dts_examiner").Alias("ex").
		Select("ex.id,ex.institution_code,u.name,ex.approval_status").
		Join("INNER", []string{"dts_user", "u"}, "ex.user_id=u.id")
	if province == "210000" {
		session = session.Where("ex.school_id=? and ex.teaching_car_type=? and ex.institution_code >?", school_id, "A1", 0)
	} else if province == "410000" || province == "520000" {
		session = session.Where("ex.school_id=?", school_id)
	} else {
		session = session.Where("ex.school_id=? and ex.approval_status=?", school_id, "1")
	}
	if _, err = session.Desc("ex.id").Limit(1).Get(&stageex); err != nil {
		return stageex, err
	}
	return stageex, nil
}
